<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class GoodsContractModel extends Model
{
    protected $table = 'edu_goods_contract_sign_up';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getGoodsContractLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];

        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
            if (get_current_type() == 4 || get_current_type() == 5) {
                $where['campus_id'] = get_current_campus_id();
            } elseif (get_current_type() == 2 || get_current_type() == 3) {
                $where['campus_id'] = ['in', get_current_campus_id()];
            }
        }
        //时间段
        if ((isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date'])) && '' !== $param['end_date'] || (isset($param['contract_number']) && '' !== $param['contract_number'])) {

            if(isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date'])){
                $where_contract['signing_date'] = array('between', array($param['start_date'],$param['end_date']));
            }

            if (isset($param['contract_number']) && '' !== $param['contract_number']) {
                $where_contract['contract_number'] = ['like','%'.$param['contract_number'].'%'];
            }

            $result_contract = ContractModel::where($where_contract)->column('id');
            $where['contract_id'] = array('in',implode(',',$result_contract));
        }





            $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $res_goods = GoodsModel::get(['id' => $item['goods_id']]);
                $item['goods_name'] = $res_goods['goods_name'];
                if($res_goods['type'] == 0){
                    $item['type_text'] = '<span style="color:#FF5722;font-weight: bold;">物品</span>';
                }else if($res_goods['type'] == 1){
                    $item['type_text'] = '<span style="color:#01AAED;font-weight: bold;">杂费</span>';
                }
                $item['unit'] = $res_goods['unit'];
                $item['nums'] = $item['goods_num'];
                $res_contract = ContractModel::get(['id' => $item['contract_id']]);
                $item['contract_number'] = $res_contract['contract_number'];
                $item['student_name'] = StudentModel::get(['id' => $item['student_id']])['student_name'];
                $item['campus'] = CampusModel::where('id',$item['campus_id'])->value('campus_name');
                if($res_contract['input_type'] == 0){
                    $item['input_type_text'] = '<span style="color:#FF5722;font-weight: bold;">新生</span>';
                }else if($res_contract['input_type'] == 1){
                    $item['input_type_text'] = '<span style="color:#01AAED;font-weight: bold;">老生</span>';
                }
                $item['signing_date'] = $res_contract['signing_date'];
                if($res_contract['status'] == 1){
                    $item['status_course_text'] = '<span style="color:#01AAED;font-weight: bold;">已付款</span>';
                }else if($res_contract['status'] == 2){
                    $item['status_course_text'] = '<span style="color:#FF5722;font-weight: bold;">未付款</span>';
                }else if($res_contract['status'] == 3) {
                    $item['status_course_text'] = '<span style="color:#FFB800;font-weight: bold;">部分付款</span>';
                }

                $total_amount = $item['net_receipts'] - $item['assessed_amount'];
                $item['total_amount'] = Number_format($total_amount / 100,2);
                $item['contract_type'] =  DictionaryModel::where('id',$item['contract_type'])->value('name');
            }
        }

        return $listData;
    }


}
